<?php
namespace Admin\Model;

use Think\Model\RelationModel;

class WxEstateModel extends RelationModel
{
    protected $connection = array(
        'db_type'       =>  'mysql',
        'db_host'       =>  '114.55.30.131',
        'db_port'       =>  '3306',
        'db_name'       =>  'wxsmart',
        'db_user'       =>  'zhkj_crm',
        'db_pwd'        =>  'AdkdCGP7fHcjht5c',
        'db_charset'    =>  'utf8',
        'db_debug'      =>  true,
    );
    protected $tablePrefix = '';
    protected $tableName = 'tp_user_estate';

    protected $_auto = array(
    );

    protected $_link = array(
    );

    public function search($pageSize = 10){
        $isverify = I('isverify');
        $isdelete = I('isdelete');
        $isdelete = $isdelete?$isdelete:0;
        $key = I('key');

        $where = whereTime('a.createtime');
        if(is_numeric($isverify))
            $where['a.isverify'] = intval($isverify);
        if(is_numeric($isdelete))
            $where['a.isdelete'] = intval($isdelete);
//        if($key)
//            $where['car_no'] = array('like',"%$key%");

        $count = $this->alias('a')->join('left join tp_userinfo b on a.wecha_id=b.wecha_id')->where($where)->count();
        $page = new \ZL\Page($count, $pageSize, C('PAGE_01'));
        $data['page'] = $page->show();

        /************************************** 取数据 ******************************************/
        $orderData = $this->alias('a')->field('a.*,b.tel,b.sex,b.customer_id,b.realname,b.wechaname')->join('left join tp_userinfo b on a.wecha_id=b.wecha_id')->where($where)->order('a.createtime desc')->limit($page->limit.','.$page->listRows)->select();
        $data['data'] = $this->strChange($orderData);
        return $data;
    }
    public function strChange($data){
        foreach ($data as $k=>&$v) {
            if($v['room_id'])
                $v['community_name'] = M('Community')->find(M('Room')->find($v['room_id'])['community_id'])['name'];
            if($v['createtime'])
                $v['create_time'] = date('Y-m-d H:i:s',$v['createtime']);
            switch ($v['isverify']){
                case 0:
                    $v['isverify_title'] = '未审核';
                    break;
                case 1:
                    $v['isverify_title'] = '已通过';
                    break;
                case -1:
                    $v['isverify_title'] = '不通过';
                    break;
            }
        }
        return $data;
    }

    public function pass(){
        $id = I('get.id');
        $data['isverify'] = 1;
        $ok = $this->data($data)->where(['id'=>$id])->save();
        if($ok){
            $this->saveEstate($id);
            $res['status'] = 1;
            $res['msg'] = '操作成功';
        }else{
            $res['status'] = false;
            $res['msg'] = '操作失败';
        }
        return $res;
    }

    public function notPass(){
        $id = I('get.id');
        $data['isverify'] = -1;
        $ok = $this->data($data)->where(['id'=>$id])->save();
        if($ok){
            $this->deleteEstate($id);
            $res['status'] = 1;
            $res['msg'] = '操作成功';
        }else{
            $res['status'] = false;
            $res['msg'] = '操作失败';
        }
        return $res;
    }

    public function saveEstate($id){
        $es_data = $this->find($id);
        $room_id = $es_data['room_id'];
        $family_id  = M('Family')->where(['room_id'=>$room_id,'status'=>1])->find()['id'];
//        myDump($room_id);
//        myDump($family_id);
        $wx_cus = D('Wxuser')->where(['wecha_id'=>$es_data['wecha_id']])->find();
        $crm_cus = M('Customer')->find($wx_cus['crm_id']);
        $member_data = M('FamilyMember')->where(['family_id'=>$family_id,'customer_id'=>$wx_cus['crm_id']])->find();
        switch ($es_data['type']){
            case 1:
                $prefix = '成员';
                break;
            case 2:
                $prefix = '业主';
                break;
            case 3:
                $prefix = '租客';
                break;
        }
        if ($prefix == '户主') {
            M('FamilyMember')->where(array('family_id' => array('eq', $family_id), 'customer_id' => array('neq', $crm_cus['id']), 'prefix'=>'户主'))->save(array('prefix' => '成员'));
            M('Room')->data(array('householder_name'=>$crm_cus['name'],'householder_mobi'=>$crm_cus['attestation_tel']))->save();
        }
        if($member_data){
            $member_data['prefix'] = $prefix;
            $member_data['estate_id'] = $wx_cus['id'];
            M('FamilyMember')->data($member_data)->save();
        }else{
            $member_data['prefix'] = $prefix;
            $member_data['estate_id'] = $wx_cus['id'];
            $member_data['family_id'] = $family_id;
            $member_data['customer_id'] = $wx_cus['crm_id'];
            $member_data['name'] = $crm_cus['name'];
            $member_data['create_time'] = time();
            $member_data['role'] = $es_data['type'];
//            myDump($member_data);
            M('FamilyMember')->add($member_data);
        }
    }

    public function deleteEstate($id){
        $es_data = $this->find($id);
        $room_id = $es_data['room_id'];
        $family_id  = M('Family')->where(['room_id'=>$room_id,'status'=>1])->find()['id'];
//        myDump($room_id);
//        myDump($family_id);
        $wx_cus = D('Wxuser')->where(['wecha_id'=>$es_data['wecha_id']])->find();
        $crm_cus = M('Customer')->find($wx_cus['crm_id']);
        M('FamilyMember')->where(['family_id'=>$family_id,'customer_id'=>$wx_cus['crm_id']])->delete();
    }

    public function _before_insert($data, $options)
    {
        parent::_after_insert($data, $options); // TODO: Change the autogenerated stub
        $data['createtime'] = time();
    }
}
